package com.slunk.pavornocturnus;

import java.util.HashMap;
import java.util.Map;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.Timer.Task;
import com.slunk.platforms.SActingObject;

public class PNDoorMonster {
	Array <Map<String,Object>> monsterIndicators;
	Array <Map<String,Object>> monsterInRoomSounds;
	PNDoorMonsterEvents eventHandler;
	Sound doorOpenSound;
	float durationOpenSound;
	Sound doorCloseSound;
	float durationCloseSound;
	Timer soundTimer;
	Timer doorOpenTimer;
	Timer doorCloseTimer;
	Timer queueTimer;
	boolean isPerforming = false;
	boolean isQueuedToPerform = false;
	SActingObject doorActingObject;
	int openDoorState;
	int closeDoorState;
	boolean loop = false;
	
	public PNDoorMonster(SActingObject doorActingObject, Sound firstAudioIndicator, float duration1,  Sound doorOpenSound, float duration2, Sound doorCloseSound, float duration3, int doorOpenState, int doorCloseState, boolean loop) {
		monsterIndicators = new Array<Map<String,Object>>();
		Map<String, Object> indicator = new HashMap<String,Object>();
		indicator.put("duration", new Float(duration1));
		indicator.put("sound", firstAudioIndicator);
		monsterIndicators.add(indicator);
		monsterInRoomSounds = new Array<Map<String,Object>>();
		this.doorOpenSound = doorOpenSound;
		this.durationOpenSound = duration2;
		this.doorCloseSound = doorCloseSound;
		this.durationCloseSound = duration3;
		this.doorActingObject = doorActingObject;
		this.openDoorState = doorOpenState;
		this.closeDoorState = doorCloseState;
		this.loop = loop;
	}
	
	public PNDoorMonster(SActingObject doorActingObject, Sound firstAudioIndicator, float duration1,  Sound doorOpenSound, float duration2, Sound doorCloseSound, float duration3) {
		this(doorActingObject,firstAudioIndicator,duration1,doorOpenSound,duration2,doorCloseSound,duration3,0,1,true);
	}
	
	public void beginActing() {
		queueTimer = new Timer();
		queueTimer.scheduleTask(new Task() {  
            @Override  
            public void run() {
            	setIsQueuedToPerform(true);
            }
        }, MathUtils.random(5.0f, 10.0f));
	}
	
	public void reset() {
		//System.out.println("reset");
		doorActingObject.activatePreviousState();
	}
	
	public PNDoorMonster(SActingObject doorActingObject, Sound firstAudioIndicator, float duration, int openDoorState, int closeDoorState) {
		this(doorActingObject,firstAudioIndicator,duration,Gdx.audio.newSound(Gdx.files.internal("sounds/door_open_sound.wav")),1.0f,Gdx.audio.newSound(Gdx.files.internal("sounds/door_close_sound.wav")), 1.0f, openDoorState, closeDoorState, true);
	}
	
	public void setIsPerforming(boolean isPerforming) {
		System.out.println("setIsPerforming");
		System.out.println(isPerforming);
		this.isPerforming = isPerforming;
	}
	
	public boolean isMonsterPerforming() {
		return isPerforming;
	}
	
	public void setIsQueuedToPerform(boolean isQueuedToPerform) {
		this.isQueuedToPerform = isQueuedToPerform;
	}
	public boolean isQueuedToPerform() {
		return isQueuedToPerform;
	}
	
	public void setEventHandler(PNDoorMonsterEvents eventHandler) {
		this.eventHandler = eventHandler;
	}
	
	public void addAudioIndicators(String pathToSound, float duration) {
		Sound sound = Gdx.audio.newSound(Gdx.files.internal(pathToSound));
		addAudioIndicators(sound,duration);
	}
	public void addAudioIndicators(Sound sound, float duration) {
		Map<String, Object> indicator = new HashMap<String,Object>();
		indicator.put("duration", new Float(duration));
		indicator.put("sound", sound);
		monsterIndicators.add(indicator);
	}
	
	public void addInRoomSounds(String pathToSound, float duration) {
		Sound sound = Gdx.audio.newSound(Gdx.files.internal(pathToSound));
		addInRoomSounds(sound,duration);
	}
	public void addInRoomSounds(Sound sound, float duration) {
		Map<String, Object> indicator = new HashMap<String,Object>();
		indicator.put("duration", new Float(duration));
		indicator.put("sound", sound);
		monsterInRoomSounds.add(indicator);
	}
	
	public void beginMonsterApproaches() {
		setIsPerforming(true);
		setIsQueuedToPerform(false);
		
		if (this.eventHandler != null) {
			this.eventHandler.indicatorSoundStarted();
		}
		Map<String,Object> indicator = monsterIndicators.random();
		Float duration = (Float)indicator.get("duration");
		Sound sound = (Sound)indicator.get("sound");
		sound.play();

		soundTimer = new Timer();
		soundTimer.scheduleTask(new Task() {  
            @Override  
            public void run() {
            	if (eventHandler != null) {
            		eventHandler.indicatorSoundEnded();
            	} 	
            }
        }, duration.floatValue());
	}
	
	public void beginMonsterInRoom() {
		if (this.eventHandler != null) {
			this.eventHandler.inRoomSoundStarted();
		}
		Map<String,Object> indicator = monsterInRoomSounds.random();
		Float duration = (Float)indicator.get("duration");
		Sound sound = (Sound)indicator.get("sound");
		sound.play();
		soundTimer = new Timer();
		soundTimer.scheduleTask(new Task() {  
            @Override  
            public void run() {
            	if (eventHandler != null) {
            		eventHandler.inRoomSoundEnded();
            	} 	
            }
        }, duration.floatValue());
	}
	
	public void beginOpenDoorSqeuence() {
		System.out.println("beginOpenDoorAnimation");
		if (this.eventHandler != null) {
			this.eventHandler.doorOpenSoundStarted();
			this.eventHandler.openDoorSequenceStarted();
		}
		doorActingObject.activateStateAt(openDoorState);
		playOpenDoorSound();
	}
	
	public void playOpenDoorSound() {
		doorOpenSound.play();
		doorOpenTimer = new Timer();
		doorOpenTimer.scheduleTask(new Task() {  
            @Override  
            public void run() {
            	if (eventHandler != null) {
            		eventHandler.doorOpenSoundEnded();
            		eventHandler.openDoorSequenceEnded();
            	} 	
            }
        }, durationOpenSound);
	}
	
	public void beginCloseDoorSqeuence () {
		if (this.eventHandler != null) {
			this.eventHandler.closeDoorSequenceStarted();
			this.eventHandler.doorCloseSoundStarted();
		}		
		doorActingObject.activateStateAt(closeDoorState);
		playCloseDoorSound();
	}
	
	private void playCloseDoorSound() {
		doorCloseSound.play();
		doorCloseTimer = new Timer();
		doorCloseTimer.scheduleTask(new Task() {  
            @Override  
            public void run() {
            	setIsPerforming(false);
        		setIsQueuedToPerform(false);
        		
        		if (loop) {
        			beginActing();
        		}
        		
        		System.out.println("hello!");
        		
            	if (eventHandler != null) {
            		eventHandler.doorCloseSoundEnded();
            		eventHandler.closeDoorSequenceEnded();	
            	} 	
            }
        }, durationCloseSound);
	}
}
